package com.aaa.jvmtest.directmemory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * 直接内存泄露
 * 反复分配空间
 *
 * @author Tu
 * @date 2022/6/15 14:54
 */
public class DirectOutOfMemory {

    private static final int _100M = 1024 * 1024 * 100;

    public static void main(String[] args) {
        List<ByteBuffer> list = new ArrayList<>();
        int i = 0;
        try {
            while (true) {
                //分配空间
                //注意方法不要用错，allocate会报堆内存溢出
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100M);
                list.add(byteBuffer);
                i++;
            }
        } finally {
            System.out.println(i);
        }
    }

}
